package com.xilihui.xlh.core.exception;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;

import com.xilihui.xlh.R;


public class ExceptionManager {

    private final ExceptionLayout exceptionLayout;

    private ExceptionLayout getExceptionLayout(Context context) {
        ExceptionLayout exceptionLayout = new ExceptionLayout(context);
        exceptionLayout.setLoadingView(getLoadingView());
        exceptionLayout.setEmptyView(getEmptyView());
        exceptionLayout.setRetryView(getRetryView());
        return exceptionLayout;
    }

    protected
    @LayoutRes
    int getLoadingView() {
        return R.layout.base_loading;
    }
    
    protected
    @LayoutRes
    int getEmptyView() {
        return R.layout.base_empty;
    }
    
    protected
    @LayoutRes
    int getRetryView() {
        return R.layout.base_retry;
    }

    public ExceptionManager(Context context, View view, OnExceptionListener listener) {
        exceptionLayout = getExceptionLayout(context);
        exceptionLayout.setContentView(view);
        exceptionLayout.showContent();
        if (listener != null) {
            listener.onEmpty(exceptionLayout.getEmptyView());
            listener.onLoading(exceptionLayout.getLoadingView());
            listener.onRetry(exceptionLayout.getRetryView());
        }
    }

    public ExceptionManager(View view, OnExceptionListener listener) {

        ViewGroup parent = (ViewGroup) view.getParent();
        int index = 0;
        for (int i = 0; i < parent.getChildCount(); i++) {
            if (parent.getChildAt(i) == view) {
                // 找到view在parent中的位置
                index = i;
                break;
            }
        }

        ViewGroup.LayoutParams lp = view.getLayoutParams();

        ExceptionLayout exceptionLayout = getExceptionLayout(view.getContext());
        // 在原来的view之前插入ExceptionLayout
        parent.removeView(view);
        parent.addView(exceptionLayout, index, lp);
        exceptionLayout.setContentView(view);
        exceptionLayout.showContent();

        if (listener != null) {
            listener.onEmpty(exceptionLayout.getEmptyView());
            listener.onLoading(exceptionLayout.getLoadingView());
            listener.onRetry(exceptionLayout.getRetryView());
        }

        this.exceptionLayout = exceptionLayout;
    }

    public interface OnExceptionListener {
        void onRetry(View retryView);

        void onEmpty(View emptyView);

        void onLoading(View loadingView);
    }

    public static ExceptionManager initialize(View view, OnExceptionListener listener) {
        return new ExceptionManager(view, listener);
    }

    public ExceptionLayout getExceptionLayout() {
        return exceptionLayout;
    }

    public void showEmpty() {
        exceptionLayout.showEmpty();
    }

    public void showRetry() {
        exceptionLayout.showRetry();
    }

    public void showLoading() {
        exceptionLayout.showLoading();
    }

    public void hide() {
        exceptionLayout.showContent();
    }
}
